/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewDecAlgJPanel.java
 *
 * Created on Jun 24, 2011, 5:56:08 AM
 */

package presentation.guiForPredictionAlgorithmEvaluation;

import business.algorithm.predictAlgorithm.AbstractPredictAlgorithm;
import java.util.TreeMap;
import javax.swing.JDialog;
import presentation.ParameterJPanel;

/**
 * 
 * @author Dinh
 */
public class AddNewPreAlgJPanel extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JDialog parent;
	private boolean ok = false;

	public boolean isOk() {
		return ok;
	}

	/** Creates new form AddNewDecAlgJPanel */
	public AddNewPreAlgJPanel(JDialog jDialog) {
		this.parent = jDialog;
		initComponents();
		initOtherComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		preAlgJComboBox = new javax.swing.JComboBox();
		preAlgParametersContainerJPanel = new javax.swing.JPanel();
		cancelJButton = new javax.swing.JButton();
		okJButton = new javax.swing.JButton();

		setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Prediction Algorithm",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION,
				new java.awt.Font("Tahoma", 1, 11))); // NOI18N

		preAlgJComboBox
				.setModel(new javax.swing.DefaultComboBoxModel(
						business.algorithm.predictAlgorithm.PredictAlgorithmAPI.PREDICTION_ALGORITHM_LIST));
		preAlgJComboBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				preAlgJComboBoxActionPerformed(evt);
			}
		});

		preAlgParametersContainerJPanel
				.setLayout(new java.awt.GridLayout(1, 0));

		cancelJButton.setText("Cancel");
		cancelJButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				cancelJButtonActionPerformed(evt);
			}
		});

		okJButton.setText("OK");
		okJButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okJButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(81, 81, 81)
								.addComponent(okJButton,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										65,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(6, 6, 6)
								.addComponent(cancelJButton,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										78,
										javax.swing.GroupLayout.PREFERRED_SIZE))
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(10, 10,
																		10)
																.addComponent(
																		preAlgParametersContainerJPanel,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		200,
																		Short.MAX_VALUE))
												.addComponent(
														preAlgJComboBox,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(preAlgJComboBox,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(preAlgParametersContainerJPanel,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										0, Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(okJButton)
												.addComponent(cancelJButton))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		layout.linkSize(javax.swing.SwingConstants.VERTICAL,
				new java.awt.Component[] { cancelJButton, okJButton });

	}// </editor-fold>//GEN-END:initComponents

	private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okJButtonActionPerformed
		this.ok = true;

		preAlg = business.algorithm.predictAlgorithm.PredictAlgorithmAPI
				.getPredictionAlgorithm((String) preAlgJComboBox
						.getSelectedItem());

		// Add new prediction algorithm
		TreeMap<String, Object> valueMap = new TreeMap<String, Object>();
		for (String name : preAlg.getParametersList().keySet()) {
			valueMap.put(name, preAlgParameterJPanel.getValue(name));
		}
		preAlg.setParametersValue(valueMap);

		this.parent.dispose();
	}// GEN-LAST:event_okJButtonActionPerformed

	private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelJButtonActionPerformed
		this.ok = false;

		this.parent.dispose();
	}// GEN-LAST:event_cancelJButtonActionPerformed

	private void preAlgJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_preAlgJComboBoxActionPerformed
		preAlgParametersContainerJPanel.removeAll();
		preAlg = business.algorithm.predictAlgorithm.PredictAlgorithmAPI
				.getPredictionAlgorithm((String) preAlgJComboBox
						.getSelectedItem());
		preAlgParameterJPanel = new ParameterJPanel(preAlg.getParametersList());
		preAlgParametersContainerJPanel.add(preAlgParameterJPanel);
		preAlgParametersContainerJPanel.updateUI();
	}// GEN-LAST:event_preAlgJComboBoxActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton cancelJButton;
	private javax.swing.JButton okJButton;
	private javax.swing.JComboBox preAlgJComboBox;
	private javax.swing.JPanel preAlgParametersContainerJPanel;
	// End of variables declaration//GEN-END:variables

	private AbstractPredictAlgorithm preAlg;
	private ParameterJPanel preAlgParameterJPanel;

	public AbstractPredictAlgorithm getPreAlg() {
		return preAlg;
	}

	public JDialog getParentDialog() {
		return parent;
	}

	private void initOtherComponents() {
		preAlg = business.algorithm.predictAlgorithm.PredictAlgorithmAPI
				.getPredictionAlgorithm((String) preAlgJComboBox
						.getSelectedItem());
		preAlgParameterJPanel = new ParameterJPanel(preAlg.getParametersList());
		preAlgParametersContainerJPanel.add(preAlgParameterJPanel);
	}

}
